package com.gexiaobao.pigeon.ui.fragment.match

import android.graphics.BitmapFactory
import android.os.Bundle
import android.view.View
import com.amap.api.maps.AMap
import com.amap.api.maps.CameraUpdateFactory
import com.amap.api.maps.MapView
import com.amap.api.maps.model.BitmapDescriptorFactory
import com.amap.api.maps.model.LatLng
import com.amap.api.maps.model.Marker
import com.amap.api.maps.model.MarkerOptions
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.initClose
import com.gexiaobao.pigeon.app.model.bean.WeatherInfoResponse
import com.gexiaobao.pigeon.app.util.SettingUtil
import com.gexiaobao.pigeon.databinding.FragmentTrainingMapBinding
import com.gexiaobao.pigeon.ui.adapter.MapInfoWinAdapter
import com.gexiaobao.pigeon.ui.race.RaceViewModel
import me.hgj.jetpackmvvm.ext.nav

/**
 * @Author      : hxw
 * @Date        : 2023/6/1 11:08
 * @Describe    : 放飞地图
 */
class FragmentTrainingMap  : BaseFragment<RaceViewModel, FragmentTrainingMapBinding>(){

    private var mId = ""

    private var markerOption: MarkerOptions? = null
    private var aMap: AMap? = null
    private var mapView: MapView? = null
    private var marker: Marker? = null
    private lateinit var latLng: LatLng

    private lateinit var adapter: MapInfoWinAdapter
    private lateinit var weatherData: WeatherInfoResponse


    override fun initView(savedInstanceState: Bundle?) {
        weatherData = arguments?.getSerializable("data") as WeatherInfoResponse
        if (weatherData != null) {
            mId = weatherData.id.toString()
        }
        val title = weatherData.title
        mDatabind.includeBar.toolbar.initClose(title) {
            nav().navigateUp()
        }
        mapView = activity?.findViewById<View>(R.id.MapView) as MapView
        mapView?.onCreate(savedInstanceState)
    }

    override fun initData() {
        super.initData()
        val lat = SettingUtil.convertToDecimalByString(weatherData.flyLat)
        val lon = SettingUtil.convertToDecimalByString(weatherData.flyLon)
        latLng = LatLng(lat, lon)

        if (aMap == null) {
            aMap = mapView!!.map
            val uiSet = aMap?.uiSettings
            uiSet?.isZoomControlsEnabled = false
            addMarkersToMap()
        }
        //使地图显示在经纬度所在位置 并设置缩放级别
        aMap?.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 8F))
        //重要 创建自定义适配器
        adapter = context?.let { MapInfoWinAdapter(it, weatherData) }!!
        aMap?.setInfoWindowAdapter(adapter) //设置自定义窗口adapter
    }

    /**
     * 在地图上添加marker
     */
    private fun addMarkersToMap() {
        markerOption = MarkerOptions().icon(
            BitmapDescriptorFactory.fromBitmap(
                BitmapFactory
                    .decodeResource(resources, R.mipmap.tools_map_location)
            )
        )
            .position(latLng)
            .anchor(0.5f, 0.5f)
            .title("标题数据")
            .snippet("消息数据")
        marker = aMap!!.addMarker(markerOption)
        marker?.showInfoWindow()
    }

    /**
     * 方法必须重写
     */
    override fun onResume() {
        super.onResume()
        mapView?.onResume()
    }

    /**
     * 方法必须重写
     */
    override fun onPause() {
        super.onPause()
        mapView?.onPause()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        mapView?.onDestroy()
    }
}